package com.epichust.bootumpp.service.impl;

import com.epichust.bootumpp.model.bo.FiltersBO;
import com.epichust.bootumpp.model.bo.Umpp5TaskOrderScheduleBO;
import com.epichust.bootumpp.model.dao.Umpp5TaskOrderScheduleDao;
import com.epichust.bootumpp.model.entity.Umpp5TaskOrderSchedule;
import com.epichust.bootumpp.service.IUmpp5TaskOrderScheduleService;
import com.epichust.bootumpp.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：longyang
 * @date ：Created in 2021/1/21 17:08
 * @description：派工单排程service
 * @modified By：
 * @version: $
 */
@Service
@Transactional
public class Umpp5TaskOrderScheduleServiceImpl implements IUmpp5TaskOrderScheduleService {

    @Autowired
    private Umpp5TaskOrderScheduleDao umpp5TaskOrderScheduleDao;

    /**
     * 分页查询派工单排程
     * @param map
     * @return
     */
    @Override
    public List<Umpp5TaskOrderScheduleBO> getAllUmpp5TaskOrderSchedule(Map<String,Object> map){
        //分页条件处理
        if(map.get("rows") != null && map.get("page") != null){
            int rows = Integer.parseInt(map.get("rows").toString());
            int page = Integer.parseInt(map.get("page").toString());
            int firstLimit = (page-1)*rows;
            int lastLimit = page*rows;
            map.put("firstLimit",firstLimit);
            map.put("lastLimit",lastLimit);
        }
        //查询条件处理
        if(map.get("filters") != null){
            String filtersStr = map.get("filters").toString();
            List<FiltersBO> filters = GsonUtils.parseString2List(filtersStr,FiltersBO.class);
            for(FiltersBO bo :filters){
                if("scheduleBeginDate".equals(bo.getField()) && "ge".equals(bo.getCompare())){//排程开始时间大于
                    map.put("scheduleBeginDateGe",bo.getData());
                }else if("scheduleBeginDate".equals(bo.getField()) && "le".equals(bo.getCompare())){//排程开始时间小于
                    map.put("scheduleBeginDateLe",bo.getData());
                }else if("scheduleEndDate".equals(bo.getField()) && "ge".equals(bo.getCompare())){//排程结束时间大于
                    map.put("scheduleEndDateGe",bo.getData());
                }else if("scheduleEndDate".equals(bo.getField()) && "le".equals(bo.getCompare())){//排程结束时间小于
                    map.put("scheduleEndDateLe",bo.getData());
                }else{
                    map.put(bo.getField(),bo.getData());
                }
            }
        }
        List<Umpp5TaskOrderScheduleBO> list = umpp5TaskOrderScheduleDao.getAllUmpp5TaskOrderSchedule(map);
        if(!list.isEmpty()){
            String records = umpp5TaskOrderScheduleDao.getAllUmpp5TaskOrderScheduleRecords(map);
            list.get(0).setRecords(records);
        }
        return list;
    }

    /**
     * 派工单手动排程
     * @param list
     */
    @Override
    public void doManualSchedule(List<Umpp5TaskOrderSchedule> list) {
        for(Umpp5TaskOrderSchedule umpp5TaskOrderSchedule : list){
            umpp5TaskOrderSchedule.setModifyDate(new Date());
            umpp5TaskOrderScheduleDao.updateUmpp5TaskOrderSchedule(umpp5TaskOrderSchedule);
        }
    }

}
